package com.zm.zmgz.service;

import com.zm.zmgz.annotationzm.InformationProhibitedDisplayItem;
import com.zm.zmgz.constant.ConstantInterface;
import com.zm.zmgz.dao.EmployeeBeanMapper;
import com.zm.zmgz.pojo.EmployeeBean;
import com.zm.zmgz.pojo.EmployeeBeanExample;
import com.zm.zmgz.pojo.EmployeeQueryPromptsBean;
import com.zm.zmgz.pojo.ShowEmployeesParamBean;
import com.zm.zmgz.utils.UniversalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;


@Service
public class EmployeeService {

    @Autowired
    private EmployeeBeanMapper employeeBeanMapper;

    public String getEmpById(String id) { // 根据id获取员工对象
//        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
//        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
//        criteria.andIdcardEqualTo(id);
//        return employeeBeanMapper.selectByExample(employeeBeanExample).toString();
        EmployeeBean employeeBean = employeeBeanMapper.selectByPrimaryKey(id);
//        return employeeBean.toString();
        return employeeBean == null ? "无" : employeeBean.toString();
    }

    public List<EmployeeQueryPromptsBean> getIdOrNameOrRname(String searchOption) { // 根据select返回姓名/身份证/推荐人
        ArrayList<EmployeeQueryPromptsBean> promptsBeans = new ArrayList<>();
        if (searchOption.equals(ConstantInterface.NAME)) {
            List<String> names = employeeBeanMapper.getNames();
            for (String name : names) {
                promptsBeans.add(new EmployeeQueryPromptsBean(name));
            }
        }
        if (searchOption.equals(ConstantInterface.IDENTIFICATIONNUMBER)) {
            List<String> ids = employeeBeanMapper.getIdentificationNumbers();
            for (String id : ids) {
                promptsBeans.add(new EmployeeQueryPromptsBean(id));
            }
        }
        if (searchOption.equals(ConstantInterface.REFERRER)) {
            List<String> rnames = employeeBeanMapper.getReferrers();
            for (String rname : rnames) {
                promptsBeans.add(new EmployeeQueryPromptsBean(rname));
            }
        }
        return promptsBeans;
    }

    /*
     * 功能:获取总记录数,员工信息
     * 步骤:获取员工信息,设置总结记录数
     *      根据displayEmployees分析出生效的显示模式,创建对应的格式,记录员工信息
     * */
    public Map getEmployees(Map displayEmployees, ShowEmployeesParamBean employeesParamBean) {
        List<EmployeeBean> employeeBeanList = employeeBeanMapper.getEmployees(employeesParamBean.getSearchOption(), employeesParamBean.getSearchOptionValue(), employeesParamBean.getLimitA(), employeesParamBean.getLimitB()); // 条件查询获取记录
        for (EmployeeBean bean : employeeBeanList) {
            System.out.println(bean);
        }
//        System.out.println("displayEmployees:"+displayEmployees);
        List<String> effectiveMode = null;
        for (Object o : displayEmployees.keySet()) { // 寻找生效的模式
            switch ((String) o) { // 根据标记,使用不同的键值
                case ConstantInterface.FULLMODEWORKS:
                    effectiveMode = (List<String>) displayEmployees.get(o);
                    break;
                case ConstantInterface.THINMODEWORKS:
                    effectiveMode = (List<String>) displayEmployees.get(o);
                    break;
            }
            if (!Objects.isNull(effectiveMode)) // 如果effectiveMode成功赋值就跳出循环
                break;
        }
//        System.out.println("生效模式:"+effectiveMode);
        displayEmployees.put(ConstantInterface.NUMBEROFEMPLOYEEINFORMATION, employeeBeanMapper.countEmployeesByCondition(employeesParamBean.getSearchOption(), employeesParamBean.getSearchOptionValue())); // 设置雇员信息条数
        List<Map<String, Object>> employeeInformations = new ArrayList<>(); // 存储所有雇员信息,一个map对应一个雇员,key为数据库表字段名
        for (EmployeeBean bean : employeeBeanList) { // 按照显示模式中的顺序将填入字段信息
            Map<String, Object> employeeInformation = new HashMap<>(); // 存储一个雇员信息;map<属性名,属性值>
            for (String s : effectiveMode) {
                employeeInformation.put(s, UniversalUtils.getFieldValueByFieldName(s, bean));
            }
            employeeInformations.add(employeeInformation);
        }
        displayEmployees.put(ConstantInterface.DISPLAYEMPLOYEEINFORMATIONDATA, employeeInformations);
        return displayEmployees;
    }

    public int validateIdentificationnumber(String identificationnumber) { // 查询身份证是否存在
        EmployeeBeanExample example = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = example.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationnumber);
        return employeeBeanMapper.countByExample(example);
    }

    public int addEmployee(EmployeeBean employeeBean) {  // 增加1个员工
        return employeeBeanMapper.insert(employeeBean);
    }

    public int editEmployee(EmployeeBean employeeBean) { // 编辑1个员工的信息
        EmployeeBean employeeBean1 = employeeBeanMapper.selectByPrimaryKey(employeeBean.getIdentificationnumber()); // 根据传入对象的身份证号从数据库中查询出该对象
        Class<? extends EmployeeBean> aClass = employeeBean.getClass(); // 获取员工对象的class对象
        Field[] declaredFields = aClass.getDeclaredFields(); // 获取员工对象的所有声明属性
        for (Field declaredField : declaredFields) { // 遍历员工对象的所有声明属性
            declaredField.setAccessible(true); // 取消对属性的安全检查
            if (!declaredField.isAnnotationPresent(InformationProhibitedDisplayItem.class)) { // 如果该属性没有被该注解标注,表示该属性允许被修改
                try {
                    if (declaredField.get((Object) employeeBean) != declaredField.get(employeeBean1)) { // 如果该属性被修改了
                        declaredField.set(employeeBean1, declaredField.get(employeeBean)); // 属性值更新
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return employeeBeanMapper.updateByPrimaryKey(employeeBean1);
    }

    public int deleteEmployee(EmployeeBean employeeBean) { // 删除1个员工
        return employeeBeanMapper.deleteByPrimaryKey(employeeBean.getIdentificationnumber());
    }

    public EmployeeBean test(String identificationNumber) { // 测试方法
        return employeeBeanMapper.selectByPrimaryKey(identificationNumber);
    }

    public List<EmployeeBean> getNoLeaveEmployees() { // 获取非离职员工集合
        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
        criteria.andStatusNotEqualTo("2");
        List<EmployeeBean> employeeBeanList = employeeBeanMapper.selectByExample(employeeBeanExample);
        if (employeeBeanList.size() > 0) {
            System.out.println("非离职员工集合:" + employeeBeanList);
            return employeeBeanList;
        } else {
            System.out.println("暂无非离职员工!");
            return null;
        }
    }

    /***
     * @Description: 根据身份证返回姓名
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-03-26 22:21:35
     */
    public String getNameByIdentificationnumber(String identificationnumber) {
        EmployeeBean employeeBean = employeeBeanMapper.selectByPrimaryKey(identificationnumber);
        if (!Objects.isNull(employeeBean)) {
            System.out.println("身份证：" + identificationnumber + " 姓名：" + employeeBean.getName());
            return employeeBean.getName();
        } else {
            System.out.println("不存在该员工（" + identificationnumber + "）");
            return null;
        }
    }

    /***
     * @Description:根据批次id获取姓名可选项
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-03-31 10:19:27
     */
    public List<Map<String, String>> getNameOptions(List<String> list) {
        List<EmployeeBean> employeeBeanList = null;
        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
        if (Objects.isNull(list) || list.size() == 0) {
            criteria.andStatusNotEqualTo("2");
        } else {
            criteria.andIdentificationnumberNotIn(list).andStatusNotEqualTo("2");
        }
        employeeBeanList = employeeBeanMapper.selectByExample(employeeBeanExample);
        System.out.println("姓名可选员工:" + employeeBeanList);
        List<Map<String, String>> mapList = new ArrayList<>();
        if (!Objects.isNull(employeeBeanList) && employeeBeanList.size() > 0) {
            for (EmployeeBean employeeBean : employeeBeanList) {
                String name = employeeBean.getName();
                String identificationnumber = employeeBean.getIdentificationnumber();
                Map<String, String> stringMap = new HashMap<>();
                stringMap.put("value", name + "(" + identificationnumber + ")");
                mapList.add(stringMap);
            }
            System.out.println("姓名可选项:" + mapList);
            return mapList;
        } else {
            return null;
        }
    }

    /***
     * @Description: 通过身份证获取员工对象
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-21 11:10:51
     */
    public EmployeeBean getEmployeeByIdentificationnumber(String identificationnumber) {
        return employeeBeanMapper.selectByPrimaryKey(identificationnumber);
    }

    /***
     * @Description: 判断员工是否在试用
     * 通过身份证获取EmployeeBean
     * 在通过status字段判断是否在试用期(状态 | 描述职工的状态：试用0，转正1，离职2，共3个状态。)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-28 08:50:28
     */
    public Boolean isProbationByIdCard(String idcard) {
        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
        criteria.andIdentificationnumberEqualTo(idcard);
        List<EmployeeBean> employeeBeanList = employeeBeanMapper.selectByExample(employeeBeanExample);
        if (Objects.isNull(employeeBeanList) || employeeBeanList.size() == 0)
            return null;
        EmployeeBean employeeBean = employeeBeanList.get(0);
        String status = employeeBean.getStatus();
        if (status.equals("0"))
            return true;
        else
            return false;

    }

    /***
     * @Description: 判断员工是否转正
     * 通过身份证获取EmployeeBean
     * 在通过status字段判断是否在试用期(状态 | 描述职工的状态：试用0，转正1，离职2，共3个状态。)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-28 08:59:45
     */
    public Boolean isFormalByIdCard(String idcard) {
        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
        criteria.andIdentificationnumberEqualTo(idcard);
        List<EmployeeBean> employeeBeanList = employeeBeanMapper.selectByExample(employeeBeanExample);
        if (Objects.isNull(employeeBeanList) || employeeBeanList.size() == 0)
            return null;
        EmployeeBean employeeBean = employeeBeanList.get(0);
        String status = employeeBean.getStatus();
        if (status.equals("1"))
            return true;
        else
            return false;
    }

    /***
    * @Description: 通过身份证号获取员工状态(0=试用;1=转正;2=离职)
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-06-01 21:54:18
    */
    public String getStatusByIdCard(String idcard) {
        EmployeeBean bean = getEmployeeByIdentificationnumber(idcard);
        String status = bean.getStatus();
        if(status.equals("0"))
            return "试用";
        else if (status.equals("1"))
            return "转正";
        else
            return "离职";
    }
}
